package com.powerband.testapiandroid

import com.powerband.apiheartrate.heartrate.SleepSeg
import org.junit.Test

import org.junit.Assert.*
import java.lang.Byte
import kotlin.experimental.and

/**
 * Example local unit test, which will execute on the development machine (host).
 *
 * See [testing documentation](http://d.android.com/tools/testing).
 */
class ExampleUnitTest {
    @Test
    fun addition_isCorrect() {
        val v = 0xFE.toByte().toInt() and 0xFF
        val s = 0xFF.toByte().toInt() and 0xFF
        val uint = 0xFF.toUInt()
        assertEquals(4, 2 + 2)
    }

    @Test
    fun parseSleepData() {
        val hexSSS =
            "40 43 90 648F 01 46 01 49 001001 19 01 01 30 02 01 3A 01 02 0F 02 02 1C 01 03 03 02 03 0E 01 03 18 02 03 20 01 04 04 02 04 0D 01 04 33 02 05 01 01 05 1A 02 05 26 01 08 04 00 FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FFC0 94 91 6477 01 09 01 6E 00 16 01 2A 01 02 03 02 02 16 01 03 0A 02 03 19 01 03 26 02 03 30 01 04 00 02 04 0D 01 04 1D 02 04 25 01 05 0B 02 05 15 01 05 25 02 05 2E 01 05 33 02 05 3B 01 06 10 02 06 18 01 07 05 02 07 0F 01 07 39 00 FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF"
        val hexString = hexSSS.replace(" ", "")
        val byteArray = ByteArray(hexString.length / 2)

        for (i in byteArray.indices) {
            val index = i * 2
            val byteStr = hexString.substring(index, index + 2)
            byteArray[i] = byteStr.toInt(16).toByte()
        }
        val dataBuffer = byteArray.toList()

        val days = dataBuffer.size / 191
        for (i in 0 until days) {
            if ((i + 1) * 191 > dataBuffer.size) {
                continue
            }
            val data = dataBuffer.subList(i * 191, (i + 1) * 191)
            val time = (data[0].toInt() and 0xFF) or
                    (data[1].toInt() and 0xFF).shl(8) or
                    (data[2].toInt() and 0xFF).shl(16) or
                    (data[3].toInt() and 0xFF).shl(24)
            val slpAll = (data[5].toInt() and 0xFF).shl(8) or (data[4].toInt() and 0xFF)
            val lowAll = (data[7].toInt() and 0xFF).shl(8) or (data[6].toInt() and 0xFF)
            val deepAll = (data[9].toInt() and 0xFF).shl(8) or (data[8].toInt() and 0xFF)

            val totalSeg = data[10].toInt() and 0xFF

            val tempSeg: MutableList<SleepSeg> = ArrayList()
            var curIndex = 0

            if (totalSeg > 60) {
                continue
            }
            val subD = dataBuffer.subList(
                11,
                11 + 3 * totalSeg
            )
            println(subD)

            while (curIndex <= subD.size - 3) {
                val triSub = subD.subList(curIndex, curIndex + 3)
                curIndex += 3
                val str = String.format(
                    "%02d:%02d:00",
                    triSub[0].toInt() and 0XFF,
                    triSub[1].toInt() and 0XFF,
                )
                println(str)
            }
        }
    }
    private fun fetchDateStr(
        y: Int,
        y1: Int,
        m: Int,
        m1: Int,
        d: Int,
        d1: Int,
        h: Int,
        mi: Int
    ): String {
        return if (h >= 20) String.format(
            "%04d-%02d-%02d %02d:%02d:00",
            y,
            m,
            d,
            h,
            m
        ) else String.format("%04d-%02d-%02d %02d:%02d:00", y1, m1, d1, h, m)
    }
}